package com.pshcp.doctor.tool;

import com.framework.base.BaseDTO;
import com.framework.exception.BizException;
import com.framework.thread.ReqComTool;
import com.framework.tool.CommonTool;
import com.framework.util.DateUtil;
import com.framework.util.SystemUtil;
import com.pshcp.doctor.ao.article.DoctorArticlePageAO;
import com.pshcp.doctor.ao.article.DoctorDraftArticlePageAO;
import com.pshcp.doctor.ao.article.PublishDoctorArticleAO;
import com.pshcp.doctor.ao.article.SaveDoctorArticleAO;
import com.pshcp.domain.dto.DoctorArticleDTO;
import com.pshcp.domain.model.Doctor;
import com.pshcp.domain.model.DoctorArticle;
import com.pshcp.domain.model.DoctorArticleNotificationPatient;
import com.pshcp.domain.model.DoctorDept;
import com.pshcp.domain.model.DoctorPatient;
import com.pshcp.domain.model.DoctorPatientGroupUser;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description: TODO
 * @Author: xiaoche
 * @Date: 2022/11/3 15:17
 */
public class DoctorArticleTool {

    public static DoctorArticleDTO buildGetDoctorArticlePage(List<Long> deptIds, DoctorArticlePageAO doctorArticlePageAO) {
        BaseDTO baseDTO = CommonTool.buildBaseDTO(doctorArticlePageAO.getPageNum(), doctorArticlePageAO.getPageSize());
        DoctorArticleDTO doctorArticleDTO = new DoctorArticleDTO();
        doctorArticleDTO.setPageNum(baseDTO.getPageNum());
        doctorArticleDTO.setPageSize(baseDTO.getPageSize());
        doctorArticleDTO.setSearch(doctorArticlePageAO.getSearch());
        doctorArticleDTO.setDoctorId(doctorArticlePageAO.getType() == null || doctorArticlePageAO.getType() == 0 ? ReqComTool.getUserId() : null);
        doctorArticleDTO.setDeptIds((doctorArticlePageAO.getType() != null && doctorArticlePageAO.getType() == 1) ? deptIds : null);
        doctorArticleDTO.setBizStatus(1);
        return doctorArticleDTO;
    }

    public static DoctorArticleDTO buildGetDoctorDraftArticlePage(DoctorDraftArticlePageAO doctorDraftArticlePageAO) {
        BaseDTO baseDTO = CommonTool.buildBaseDTO(doctorDraftArticlePageAO.getPageNum(), doctorDraftArticlePageAO.getPageSize());
        DoctorArticleDTO doctorArticleDTO = new DoctorArticleDTO();
        doctorArticleDTO.setPageNum(baseDTO.getPageNum());
        doctorArticleDTO.setPageSize(baseDTO.getPageSize());
        doctorArticleDTO.setSearch(doctorDraftArticlePageAO.getSearch());
        doctorArticleDTO.setDoctorId(ReqComTool.getUserId());
        doctorArticleDTO.setBizStatus(0);
        return doctorArticleDTO;
    }


    public static void checkSaveDoctorArticle(SaveDoctorArticleAO saveDoctorArticleAO) throws BizException {
        CommonTool.checkObj(saveDoctorArticleAO.getType(), "10040001");
        CommonTool.checkString(saveDoctorArticleAO.getTitle(), "10040002");
        if (saveDoctorArticleAO.getType() == 0) {
            CommonTool.checkString(saveDoctorArticleAO.getContent(), "10040004");
            CommonTool.checkObj(saveDoctorArticleAO.getVisibleRange(), "10040005");
            CommonTool.checkObj(saveDoctorArticleAO.getOriginalFlag(), "10040006");
            CommonTool.checkString(saveDoctorArticleAO.getCoverPath(), "10040007");
        }
        if (saveDoctorArticleAO.getType() == 1) {
            CommonTool.checkString(saveDoctorArticleAO.getLink(), "10040003");
        }
    }

    public static DoctorArticle buildSaveDoctorArticle(DoctorArticle doctorArticle, SaveDoctorArticleAO saveDoctorArticleAO) {
        doctorArticle.setType(saveDoctorArticleAO.getType());
        doctorArticle.setTitle(saveDoctorArticleAO.getTitle());
        doctorArticle.setContent(saveDoctorArticleAO.getContent());
        doctorArticle.setVisibleRange(saveDoctorArticleAO.getVisibleRange());
        doctorArticle.setOriginalFlag(saveDoctorArticleAO.getOriginalFlag());
        doctorArticle.setCoverPath(saveDoctorArticleAO.getCoverPath());
        doctorArticle.setLink(saveDoctorArticleAO.getLink());
        return doctorArticle;
    }

    public static DoctorArticle buildSaveDoctorArticle(DoctorDept doctorDept, SaveDoctorArticleAO saveDoctorArticleAO) {
        DoctorArticle doctorArticle = new DoctorArticle();
        doctorArticle.setBizStatus(0);
        doctorArticle.setDoctorId(ReqComTool.getUserId());
        doctorArticle.setDeptId(doctorDept == null ? null : doctorDept.getId());
        doctorArticle.setTitle(saveDoctorArticleAO.getTitle());
        doctorArticle.setContent(saveDoctorArticleAO.getContent());
        doctorArticle.setVisibleRange(saveDoctorArticleAO.getVisibleRange());
        doctorArticle.setOriginalFlag(saveDoctorArticleAO.getOriginalFlag());
        doctorArticle.setCoverPath(saveDoctorArticleAO.getCoverPath());
        doctorArticle.setType(saveDoctorArticleAO.getType());
        doctorArticle.setLink(saveDoctorArticleAO.getLink());
        return doctorArticle;
    }

    public static void checkPublishDoctorArticle(PublishDoctorArticleAO publishDoctorArticleAO) throws BizException {
        CommonTool.checkObj(publishDoctorArticleAO.getPublishType(), "10040010");
        if (publishDoctorArticleAO.getPublishType() == 0) {
            CommonTool.checkObj(publishDoctorArticleAO.getId(), "10040018");
        } else {
            CommonTool.checkObj(publishDoctorArticleAO.getType(), "10040011");
            CommonTool.checkString(publishDoctorArticleAO.getTitle(), "10040012");
            if (publishDoctorArticleAO.getType() == 0) {
                CommonTool.checkString(publishDoctorArticleAO.getContent(), "10040014");
                CommonTool.checkObj(publishDoctorArticleAO.getVisibleRange(), "10040015");
                CommonTool.checkObj(publishDoctorArticleAO.getOriginalFlag(), "10040016");
                CommonTool.checkString(publishDoctorArticleAO.getCoverPath(), "10040017");
            }
            if (publishDoctorArticleAO.getType() == 1) {
                CommonTool.checkString(publishDoctorArticleAO.getLink(), "10040013");
            }
        }
    }

    public static DoctorArticle buildPublishDoctorArticle(DoctorArticle doctorArticle) {
        doctorArticle.setBizStatus(1);
        doctorArticle.setPublishTime(DateUtil.currDate());
        return doctorArticle;
    }

    public static DoctorArticle buildPublishDoctorArticle(DoctorArticle doctorArticle, PublishDoctorArticleAO publishDoctorArticleAO) {
        DoctorArticle updateDoctorArticle = new DoctorArticle();
        updateDoctorArticle.setId(doctorArticle == null ? null : doctorArticle.getId());
        updateDoctorArticle.setDoctorId(doctorArticle == null ? ReqComTool.getUserId() : doctorArticle.getDoctorId());
        updateDoctorArticle.setDeptId(doctorArticle == null ? publishDoctorArticleAO.getDeptId() : doctorArticle.getDeptId());
        updateDoctorArticle.setStatus(0);
        updateDoctorArticle.setBizStatus(1);
        updateDoctorArticle.setPublishTime(DateUtil.currDate());
        updateDoctorArticle.setType(publishDoctorArticleAO.getType());
        updateDoctorArticle.setTitle(publishDoctorArticleAO.getTitle());
        updateDoctorArticle.setContent(publishDoctorArticleAO.getContent());
        updateDoctorArticle.setVisibleRange(publishDoctorArticleAO.getVisibleRange());
        updateDoctorArticle.setOriginalFlag(publishDoctorArticleAO.getOriginalFlag());
        updateDoctorArticle.setCoverPath(publishDoctorArticleAO.getCoverPath());
        updateDoctorArticle.setLink(publishDoctorArticleAO.getLink());
        return updateDoctorArticle;
    }

    public static List<DoctorArticleNotificationPatient> buildSaveDoctorArticleNotificationPatientList(List<DoctorArticleNotificationPatient> doctorArticleNotificationPatientList) {
        for (DoctorArticleNotificationPatient doctorArticleNotificationPatient : doctorArticleNotificationPatientList) {
            doctorArticleNotificationPatient.setUpdateTime(DateUtil.currDate());
            doctorArticleNotificationPatient.setBizStatus(1);
        }
        return doctorArticleNotificationPatientList;
    }

    public static List<DoctorArticleNotificationPatient> buildSaveDoctorArticleNotificationPatientList(Integer bizStatus, Long doctorArticleId, List<DoctorArticleNotificationPatient> doctorArticleNotificationPatientList, List<DoctorPatient> doctorPatientList) {
        Map<Long, DoctorArticleNotificationPatient> doctorArticleNotificationPatientMap = doctorArticleNotificationPatientList.stream().collect(Collectors.toMap(DoctorArticleNotificationPatient::getPatientId, doctorArticleNotificationPatient -> doctorArticleNotificationPatient));
        List<DoctorArticleNotificationPatient> list = new ArrayList<>();
        DoctorArticleNotificationPatient doctorArticleNotificationPatient = null;
        List<Long> haveIds = new ArrayList<>();
        for (DoctorPatient doctorPatient : doctorPatientList) {
            haveIds.add(doctorPatient.getPatientId());
            DoctorArticleNotificationPatient articleNotificationPatient = doctorArticleNotificationPatientMap.get(doctorPatient.getPatientId());
            if (articleNotificationPatient != null) {
                articleNotificationPatient.setUpdateTime(DateUtil.currDate());
                list.add(articleNotificationPatient);
            } else {
                doctorArticleNotificationPatient = new DoctorArticleNotificationPatient();
                doctorArticleNotificationPatient.setId(SystemUtil.getId());
                doctorArticleNotificationPatient.setDoctorArticleId(doctorArticleId);
                doctorArticleNotificationPatient.setPatientId(doctorPatient.getPatientId());
                doctorArticleNotificationPatient.setBizStatus(bizStatus);
                doctorArticleNotificationPatient.setStatus(0);
                doctorArticleNotificationPatient.setCreatedTime(DateUtil.currDate());
                doctorArticleNotificationPatient.setUpdateTime(DateUtil.currDate());
                list.add(doctorArticleNotificationPatient);
            }
        }
        for (DoctorArticleNotificationPatient articleNotificationPatient : doctorArticleNotificationPatientList) {
            if (CollectionUtils.isEmpty(haveIds) || !haveIds.contains(articleNotificationPatient.getPatientId())) {
                articleNotificationPatient.setStatus(1);
                articleNotificationPatient.setUpdateTime(DateUtil.currDate());
                list.add(articleNotificationPatient);
            }
        }
        return list;
    }
}
